﻿// 版权所有 (c) 2021-2024 duanzt

using Admin.NET.Core.Service;
using Admin.NET.Plugin.ViewFace.Service.FaceSet.Dto;
using System.ComponentModel;

namespace Admin.NET.Plugin.ViewFace.Service;
[ApiDescriptionSettings(ViewFaceConst.GroupName, Order = 101)]
public class FaceUserService : IDynamicApiController, ITransient
{
    private readonly static string imagePath = @"images/Jay_3.jpg";
    private readonly static string outputPath = @"images/Jay_out.jpg";

    /*
     * 1.1、人脸注册
     * 1.2、人脸更新
     * 1.3、人脸删除
     * 1.4、用户信息
     * 1.5、获取用户人脸列表
     * 
     * 2.1、获取用户列表
     * 2.2、复制用户
     * 2.3、删除用户
     */

    private readonly UserManager _userManager;
    private readonly SqlSugarRepository<FaceUser> _faceUserRep;

    public FaceUserService(UserManager userManager,
        SqlSugarRepository<FaceUser> faceUserRep)
    {
        _userManager = userManager;
        _faceUserRep = faceUserRep;
    }

    /// <summary>
    /// 获取用户分页列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("获取用户分页列表")]
    public async Task<SqlSugarPagedList<FaceUser>> Page(FaceUserInput input)
    {
        return await _faceUserRep.AsQueryable()
            .WhereIF(!_userManager.SuperAdmin, u => u.TenantId == _userManager.TenantId) // 若非超管，则只能操作本租户的角色
            .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code))
            .OrderBy(u => u.Id)
            .ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Add"), HttpPost]
    public async Task AddUseGroup(AddFaceUserInput input)
    {
        var base64 = input.image_type;
        var feature = "";
        //
        var en = new FaceUser
        {
            Code = input.user_id,
            GroupCode = input.group_id,    
            Base64 = base64, 
            Feature = feature
        };

        await _faceUserRep.InsertAsync(en);
    }

    /// <summary>
    /// 更新用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Update"), HttpPost]
    [DisplayName("更新用户组")]
    public async Task UpdateUser(UpdatePosInput input)
    {
        if (await _faceUserRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id))
            throw Oops.Oh(ViewFaceEnum.D1001);

        var sysPos = await _faceUserRep.GetByIdAsync(input.Id) ?? throw Oops.Oh(ViewFaceEnum.D1002);

        await _faceUserRep.AsUpdateable(input.Adapt<FaceUser>()).IgnoreColumns(true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Delete"), HttpPost]
    public async Task DeleteUser(DeleteFaceUserInput input)
    {
        await _faceUserRep.DeleteAsync(u => u.Id == input.Id);
    }

}
